using UnityEngine;
using System.Collections;

public class TorretaControl : StateBehaviour<TorretaControl> 
{
    public float LIFE = 10f;
    public float DAMAGE = 2f;
    public float ATACKSPEED = 1f;     //mientras menor sea, mas rapido
    public GameObject bala;
	
	[HideInInspector]
	public float time;
    [HideInInspector]
    public ArrayList listaEnemigos;
    [HideInInspector]
	public Transform enemigoActual;
	[HideInInspector]
	public Transform cuerpoTorre;
	
	private VampiroControl vampiroControl;
	
    void Start () 
    {       
        this.updateCalledIn = StateBehaviourMethodsName.FixedUpdate;
        this.listaEnemigos = new ArrayList();
		this.cuerpoTorre = this.gameObject.transform.FindChild("body");
        this.State = StateTorreLook.instance;
	}

    public override void OnUpdate()
    {
    }

    public override TorretaControl GetBehaviuor()
    {
        return this;
    }

    void OnTriggerEnter(Collider other)
    {
		 if (other.name == "Vampiro(Clone)") 
		 {
             listaEnemigos.Add(other.GetComponent<Transform>());
			 Debug.Log("------Torre_entra enemigo-----> " + listaEnemigos.Count);   		 
			
             //enemigoActual = other.GetComponent<Transform>();
             //Debug.Log("enemigo visto" + enemigoActual.position);
			//this.State = StateTorreAtack.instance;    
		 }
    }
	
	void OnTriggerExit(Collider other)
    {		
		Transform exit = other.GetComponent<Transform>();
		
		if (listaEnemigos.Contains(exit))
        {
            listaEnemigos.Remove(exit);
			Debug.Log("------Torre_sale enemigo-----> " + listaEnemigos.Count); 
			if (enemigoActual == exit)
			{
				nextEnemy();
			}
        }
	}
  	
	public void RecibirDmg(float dmg)
	{
        LIFE = LIFE - dmg;
        Debug.Log("------Torre_life_left: " + LIFE);
        if (LIFE <= 0)
        {
            this.State = StateTorreDie.instance;
        }
	}

    public void Disparar()
    {
		Debug.Log("TORRE ATACK!!");
		GameObject cloneBala = (GameObject)Instantiate(bala, cuerpoTorre.FindChild("caon/Disparador").position, cuerpoTorre.rotation/*Quaternion.identity*/);          
		cloneBala.GetComponent<ImpactoBalaTorre>().damage = DAMAGE;
		cloneBala.rigidbody.AddRelativeForce(new Vector3(0,0,10), ForceMode.VelocityChange);
    }

    public void Destruir()
    {
        Debug.Log("::::::::: destruimos torre ::::::::::");
        Destroy(this.gameObject);
    }

    public bool isListaEnemigosVacia()
    {
        if (listaEnemigos.Count != 0)
        {
            return false;
        }
        return true;
    }
    
    public void nextEnemy()
    {
        if (!isListaEnemigosVacia())
        {
            enemigoActual = (Transform)listaEnemigos[0];
			this.State = StateTorreAtack.instance;
        }
        else
        {
            this.State = StateTorreLook.instance;
        }
    }

    public void EnemigoMuerto(Transform tr)
    {
		Debug.Log("torre: enemigo muerto ------------------ " + tr.name);
		if (tr.name == "Vampiro(Clone)") 
		{	        
	        if (listaEnemigos.Contains(tr))
	        {
	            listaEnemigos.Remove(tr);
	        }
			if (enemigoActual == tr)
	        {				
	            nextEnemy();
	        }
		}
    }
	
}
